package schoolsource.model;

import schoolsource.sql.DBSQLAccessor;

/**
 * 
 * @author Steven Steele
 * Copyright 2003
 *
 * Item containing a double value, database tools for saving and loading it, its required-ness, 
 * and validation tools.
 */
public class DoubleItem extends Item {

	// The value stored by the Item
	private Double value = new Double(0.00);

	/**
	 * Returns a new Item with the database field name, value, and required-ness set
	 * to the parameter values.
	 * @param newField The name of the database field where the double value is stored.
	 * @param newVal The value of the double.
	 * @param newReq Whether the double value may be unset. True indicates it may not be unset.
	 */
	public DoubleItem(String newField, String newVal, boolean newReq) {
		setFieldName(newField);
		setValue(newVal);
		setRequired(newReq);
	}

	/**
	 * Returns a new Item with the database field name, value, and required-ness set
	 * to the parameter values.
	 * @param newField The name of the database field where the double value is stored.
	 * @param newVal The value of the double.
	 * @param newReq Whether the double value may be unset. True indicates it may not be unset.
	 */
	public DoubleItem(String newField, Object newVal, boolean newReq) {
		setFieldName(newField);
		setTrueValue(newVal);
		setRequired(newReq);
	}

	/**
	 * Sets the value using a string. 
	 * @param newValue The value to set the double.
	 */
	public void setValue(String newValue) {
		try {
			setTrueValue(new Double(newValue));
		} catch (NumberFormatException nfe) {
			setErrorString("" + newValue + " must be a double.");
			setValid(false);
		}
	}

	/**
	 * Sets the value using a Double object.
	 * @param newTrueValue An object that can be cast to a Double.
	 */
	public void setTrueValue(Object newTrueValue) {
		value = (Double) newTrueValue;
	}

	/**
	 * Returns the string representation of the Item.
	 * @return The string representation of the Item.
	 */
	public String toString() {

		return String.valueOf(value);
	}

	/**
	 * Returns the object associated with the Item.
	 * @return The object associated with the Item.
	 */
	public Object getObject() {
		return value;
	}

	/**
	 * Returns a string formatted in the appropriate manner for the Item, 
	 * according to the DBSQLAccessor passed in.
	 * @param dba A DBSQLAccessor that defines how values are represented in the
	 * target database.
	 */
	public String getDBFormattedString(DBSQLAccessor dba) {
		return dba.getDBFormattedDouble(toString());
	}

	/**
	 * Sets the value of the Item according to the interpretation of a database representation
	 * of the Item using the passed-in DBSQLAccessor to interpret it.
	 * @param dbString The database representation of the Item.
	 * @param dba The DBSQLAccessor that defines how values are translated from the database
	 * representation to the Item value.
	 */
	public void createFromDBString(String dbString, DBSQLAccessor dba) {
		setValue(dba.removeDBTagsFromString(dbString));
	}

	/**
	 * Indicates whether the Item is valid. This value is current only after the Item
	 * has validate() called on it.
	 * @return A boolean indicating whether the Item is valid.
	 */
	public boolean isValid() {
		return valid;
	}

	/**
	 * Processes the Item and determines whether it is valid strictly according to rules
	 * governing the Item implementation and whether the Item is required. 
	 */
	public void validate() {
		if (toString() == null || ((String) toString()).equals("")) {
			if (isRequired()) {
				setValid(false);
				setErrorString("This field is required.");
			} else {
				setValid(true);
			}
		} else {
			setValid(true);
		}
	}
}
